var dom;

var testGoogUserAgent = function() {
  goog.array.forEach(['OPERA', 'IE', 'GECKO', 'WEBKIT', 'MAC', 'WINDOWS', 'LINUX'],
      function (name) {
        goog.dom.setTextContent(dom.$(name), goog.userAgent[name]);
      });
}

var isButtonListenerDisposeInternalCalled = false;

var showTestResult = function(isSuccess, elementId) {
  var element = dom.$(elementId);
  goog.dom.classes.add(element, isSuccess ? 'success' : 'failure');
  goog.dom.setTextContent(element, String(isSuccess));
};

var testOverridingAMethod = function() {
  var listener = new example.ButtonListener();
  var success = false;
  try {
    listener.dispose();
    success = true;
  } catch(e) {
    // OK
  }
  success = success && isButtonListenerDisposeInternalCalled;
  showTestResult(success, 'overrideTest');
};

goog.provide('example.ButtonListener');

goog.require('goog.events.EventHandler');
goog.require('goog.events.EventTarget');

/**
 * @constructor
 * @extends {goog.events.EventTarget}
 */
example.ButtonListener = function() {
  goog.events.EventTarget.call(this);

  /**
   * @type {goog.events.EventHandler}
   * @protected
   */
  this.handler = new goog.events.EventHandler(this);
};
goog.inherits(example.ButtonListener, goog.events.EventTarget);

/** @override */
example.ButtonListener.prototype.disposeInternal = function() {
  isButtonListenerDisposeInternalCalled = true;
  example.ButtonListener.superClass_.disposeInternal.call(this);

  this.handler.dispose();
};

var testCoordinateProperties = function() {
  var point = new goog.math.Coordinate(7, -7);
  var hasExpectedProperties = !!(point.x == 7 && point.y == -7);
  showTestResult(hasExpectedProperties, 'coordinateTest');
};

var testBrowserEventProperties = function() {
  var event = {
    'type': 'keypress',
    'target': document.body,
    'metaKey': true
  };
  var currentTarget = document.body;
  var browserEvent = new goog.events.BrowserEvent(event, currentTarget);
  var hasExpectedProperties = !!(browserEvent.type === 'keypress' &&
      browserEvent.target === document.body &&
      browserEvent.metaKey === true);
  showTestResult(hasExpectedProperties, 'browserEventTest');
};

var testBoxProperties = function() {
  var box = new goog.math.Box(1, 2, 3, 4);
  var hasExpectedProperties = !!(box.top === 1 && box.right === 2 &&
      box.bottom === 3 && box.left === 4);
  showTestResult(hasExpectedProperties, 'boxTest');
};

var main = function() {
  dom = new goog.dom.DomHelper(document);
  testGoogUserAgent();
  testOverridingAMethod();
  // TODO(bolinfest): Test event dispatch.

  // Properties do not work well with exports, so extra testing is required to
  // ensure that they are available via their original names.
  testCoordinateProperties();
  testBrowserEventProperties();
  testBoxProperties();
};